const UserApi = require('../api/user.js');

// 查询登录用户详情
const getLoginUser = () => {
  UserApi.getLoginUser().then(userRes=>{
    if (userRes.retCode == "000000" && userRes.errCode == "000000") {
      wx.setStorageSync('userInfo', userRes.data)
    }
  })
}
// 查询登录用户会员服务状态
const getUserServiceState = () => {
  UserApi.getUserServiceState().then(res=>{
    if (res.retCode == '000000' && res.errCode == '000000') {
      wx.setStorageSync('userService', res.data);
    }
  })
}
const logout = () => {
  wx.removeStorageSync('token')
  wx.removeStorageSync('tokenExpire')
  wx.removeStorageSync('userInfo')
  wx.removeStorageSync('userService')
}
const handleTokenExpire = (redirectUri, redirectType) => {
  wx.showToast({icon: 'none', title: '登录已过期'})
  wx.removeStorageSync('token')
  wx.removeStorageSync('tokenExpire')
  wx.removeStorageSync('userInfo')
  wx.removeStorageSync('userService')
  setTimeout(()=>{
    wx.redirectTo({
      url: '/packageA/pages/login/index?redirect_uri='+redirectUri+'&redirect_uri_type='+redirectType,
    })
  }, 2500)
}

module.exports = {
  getLoginUser,
  getUserServiceState,
  logout,
  handleTokenExpire,
}